package tmproject.hlhj.fhp.tmvote.activitys

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import kotlinx.android.synthetic.main.recy_layout.*
import tmproject.hlhj.fhp.tmvote.BaseAty
import tmproject.hlhj.fhp.tmvote.Content
import tmproject.hlhj.fhp.tmvote.R
import tmproject.hlhj.fhp.tmvote.adapters.VoteListAdp
import tmproject.hlhj.fhp.tmvote.beans.AppColorBean
import tmproject.hlhj.fhp.tmvote.beans.VoteListBean
import tmproject.hlhj.fhp.tmvote.module.VoteListModule
import tmproject.hlhj.fhp.tmvote.presenters.VoteListPresenter
import tmproject.hlhj.fhp.tmvote.views.VoteListView

/**
 * Created by Never Fear   on 2018\8\6 0006.
Never More....
 */
class VoteListAty :BaseAty< VoteListView, VoteListPresenter>(), VoteListView {
    private var page=1;
    private var pageSize=10;
    private val voteListDatas=ArrayList<VoteListBean.DataBean.ListBean>()
    private lateinit var voteListadp:VoteListAdp


    override fun createPresenter(): VoteListPresenter {
        return VoteListPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.vote_list
    }
    /*接口*/
    override fun initView() {
        spView.header=DefaultHeader(this)
        voteListadp= VoteListAdp(voteListDatas)

        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        recyclerview.adapter=voteListadp

        val listener=object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                page++
                presenter?.getVoteListData(page,pageSize,this@VoteListAty)
            }
        }
        voteListadp.setOnLoadMoreListener(listener,recyclerview)


    }

    override fun logicStart() {
        loadingDialog.show()
        presenter?.getAppColor(this)
    }

    override fun bindView(): VoteListView {
        return this
    }

    override fun setListener() {
        spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
            }

            override fun onRefresh() {
                voteListDatas.clear()
                page=1
                presenter?.getVoteListData(page,pageSize,this@VoteListAty)

            }
        })

                voteListadp.setOnItemClickListener { adapter, view, position ->
            when(voteListDatas[position].state){
                "1"->{
                    val intent = Intent(this@VoteListAty, VotingAty::class.java)
                    intent.putExtra("id",voteListDatas[position].activity_id)
                    intent.putExtra("state",1)
                    intent.putExtra("canSign",voteListDatas[position].is_enroll)
                    startActivity(intent)
                }
                "2"->{
                    val intent = Intent(this@VoteListAty, VotingAty::class.java)
                    intent.putExtra("id",voteListDatas[position].activity_id)
                    intent.putExtra("state",2)
                    intent.putExtra("canSign",voteListDatas[position].is_enroll)
                    startActivity(intent)
                }
                "3"->{
                    val intent = Intent(this@VoteListAty, VotingAty::class.java)
                    intent.putExtra("id",voteListDatas[position].activity_id)
                    intent.putExtra("state",3)
                    intent.putExtra("canSign",voteListDatas[position].is_enroll)
                    startActivity(intent)
                }
                "0"->{
                    val intent = Intent(this@VoteListAty, VotingAty::class.java)
                    intent.putExtra("id",voteListDatas[position].activity_id)
                    intent.putExtra("state",2)

                    startActivity(intent)
                }
            }

        }
    }


    override fun onVoteListResp(voteListBean: VoteListBean) {
        loadingDialog.dismiss()
        spView.onFinishFreshAndLoad()
        if (null!=voteListBean.data){
            if (voteListBean.data.list.size>0){
                if (voteListBean.data.list.size<pageSize){
                    voteListadp.loadMoreEnd()
                }else{
                    voteListadp.loadMoreComplete()
                }
            }else{
                voteListadp.loadMoreEnd()
            }
        }
        voteListDatas.addAll(voteListBean.data.list)
        voteListadp.notifyDataSetChanged()
    }

    override fun onColorBack(appColorBean: AppColorBean) {
        loadingDialog.dismiss()
        if (appColorBean.data.equals("1")){
            Content.vote_list_item_bac=R.drawable.list_bg_r//列表条目背景
            Content.vote_detail_head=R.drawable.img_bg_first_r//详情头部
            Content.vote_detail_head_women=R.drawable.img_def_r//跳舞的女人
            Content.vote_commit_bottom_bac=R.drawable.btn_tijiao_r//按钮背景
            Content.vote_rule_bac=R.drawable.rule_border_r//规则背景框
            Content.vote_text_color="#E03434"
            Content.vote_num_text_color="#FFD400"
            Content.shadow_Color="#ff0000"
        }else if (appColorBean.data.equals("2")){
            Content.vote_list_item_bac=R.drawable.list_bg//列表条目背景
            Content.vote_detail_head=R.drawable.img_bg_first//详情头部
            Content.vote_detail_head_women=R.drawable.img_def//跳舞的女人
            Content.vote_commit_bottom_bac=R.drawable.btn_tijiao//按钮背景
            Content.vote_rule_bac=R.drawable.rules_border//规则背景框
            Content.vote_text_color="#2472DD"
            Content.vote_num_text_color="#2472DD"
            Content.shadow_Color="#0657c5"
        }
        presenter?.getVoteListData(page,pageSize,this@VoteListAty)
    }
}